﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

using SBPweb.PhotoGallery.Common;
using SBPweb.PhotoGallery.Client.Logic;

namespace SBPweb.PhotoGallery.Client
{

    public partial class SlidesBar : ButtonPresenterControl
    {

        #region Private fields

        private AdvancingGallery gallery;
        private Dictionary<FetchableSlide, Border> dictButton = new Dictionary<FetchableSlide, Border>();
        private Dictionary<Border, FetchableSlide> dictSlides = new Dictionary<Border, FetchableSlide>();

        #endregion

        #region Constructor logic

        public SlidesBar()
        {
            InitializeComponent();
        }

        #endregion

        #region Property accessors

        public AdvancingGallery Gallery
        {
            get
            {
                return gallery;
            }
            set
            {
                FinalizeSlides();
                gallery = value;
                InitializeSlides();
            }
        }

        private void InitializeSlides()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ThumbnailFetching += new EventHandler<SlideFetchProgressEventArgs>(ThumbnailFetching);
            gallery.ThumbnailFetched += new EventHandler<SlideFetchEventArgs>(ThumbnailFetched);
            gallery.ItemAdded += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            gallery.ItemRemoved += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        private void FinalizeSlides()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ThumbnailFetching -= new EventHandler<SlideFetchProgressEventArgs>(ThumbnailFetching);
            gallery.ThumbnailFetched -= new EventHandler<SlideFetchEventArgs>(ThumbnailFetched);
            gallery.ItemAdded -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            gallery.ItemRemoved -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        private void AddSlide(int index, FetchableSlide slide)
        {
            Border b = new Border();
            b.Width = 48;
            b.Height = 48;
            b.Margin = new Thickness(3);
            b.CornerRadius = new CornerRadius(5);
            b.BorderThickness = new Thickness(1);
            b.BorderBrush = new SolidColorBrush(Colors.White);
            lock (dictButton)
            {
                spSlides.Children.Insert(index, b);
                dictSlides.Add(b, slide);
                dictButton.Add(slide, b);
            }
            slide.FetchThumbnail();
        }

        private void RemoveSlide(int index)
        {
            Border b = spSlides.Children[index] as Border;
            lock (dictButton)
            {
                FetchableSlide slide = dictSlides[b];
                dictButton.Remove(slide);
                dictSlides.Remove(b);
                spSlides.Children.Remove(b);
            }
        }

        private void SetBackground(Border border, Stream image)
        {
            ImageBrush iB = new ImageBrush();
            BitmapImage bI = new BitmapImage();
            bI.SetSource(image);
            iB.ImageSource = bI;
            iB.Stretch = Stretch.UniformToFill;
            border.Background = iB;
        }

        #endregion

        #region Event handlers

        private void SlideAdded(object sender, OverridableList<FetchableSlide>.ItemEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                AddSlide(e.Index, e.Item);
            });
        }

        private void SlideRemoved(object sender, OverridableList<FetchableSlide>.ItemEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                RemoveSlide(e.Index);
            });
        }

        private void ThumbnailFetching(object sender, SlideFetchProgressEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                lock (dictButton)
                {
                    if (!dictButton.ContainsKey(e.Slide))
                    {
                        return;
                    }
//                    Border b = dictGrids[e.Slide];
                }
            });
        }

        private void ThumbnailFetched(object sender, SlideFetchEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                lock (dictButton)
                {
                    if (!dictButton.ContainsKey(e.Slide))
                    {
                        return;
                    }
                    SetBackground(dictButton[e.Slide], e.Slide.Thumbnail);
                }
            });
        }

        #endregion

    }

}